#
# Filename: CommonCallWrapper.py
# $Id: CommonCallWrapper.py,v 1.1 1999/08/13 12:39:52 toni Exp $
#
# define a common class for using different calling
# possibilities. This class can handle local calls,
# remote calls and DLL calls
#
# Author: Uwe Stieber
# Copyright (c) 1998, 1999 TakeFive Software, Salzburg
#

# import the necessary modules

# some global variables
TEST = 0

# define some global constants
LOCAL		= 0
REMOTE		= 1
SHAREDLIB	= 2

# define the CommonCallWrapper class
class CommonCallWrapper:
	# some class privates
	__interfaceUsed = 0		# 0 ... local calls
							# 1 ... remote calls
							# 2 ... DLL shared library calls
	__toolName = None		# Name of the used VCS tool
	__toolModule = None		# tool module witch contains the real calls
	__toolModuleName = None	# tool module name, will build by using __toolname +
							# 'Local', 'Remote' or 'SharedLib' depend on __interfaceUsed

	def __init__(self, toolName, interface = LOCAL):
		if TEST:
			print "(@B@I%s@I@B)@B@g>>>@x __init__(%s, %d)@B" % (__name__, toolName, interface)
		# set the variables
		self.__toolName = toolName
		self.__interfaceUsed = interface
		if self.__toolName != None and self.__interfaceUsed >= 0 and self.__interfaceUsed <=2:
			if self.__interfaceUsed == LOCAL:
				self.__toolModuleName = "%sLocal" % self.__toolName
			if self.__interfaceUsed == REMOTE:
				self.__toolModuleName = "%sRemote" % self.__toolName
			if self.__interfaceUsed == SHAREDLIB:
				self.__toolModuleName = "%sSharedLib" % self.__toolName
			# try to import the module
			if self.__toolModuleName != None:
				try:
					if TEST:
						print "(@B@I%s@I@B)\ttry to import module @B%s@B..." % (__name__, self.__toolModuleName)
					self.__toolModule = __import__(self.__toolModuleName)
				except ImportError:
					if TEST:
						print "(@B@I%s@I@B)\t\t@B@rfailed@x@B" % __name__
						print "(@B@I%s@I@B)@B@r<<<@x __init__ (Exception)@B" % __name__
					self.__toolModule = None
					self.__toolModuleName = ""
					raise CommonCallWrapperInterfaceImportFailed
				if TEST:
					print "(@B@I%s@I@B)\t\t@B@gsuccessfully@x@B" % __name__
			else:
				if TEST:
					print "(@B@I%s@I@B)\tno module name set for import !" % __name__
					print "(@B@I%s@I@B)@B@r<<<@x __init__ (Exception)@B" % __name__
				raise CommonCallWrapperInterfaceImportFailed
		else:
			if TEST:
				print "(@B@I%s@I@B)\tNo tool name available or invalid interface flag !" % __name__
				print "(@B@I%s@I@B)@B@r<<<@x __init__ (Exception)@B" % __name__
			raise CommonCallWrapperInitFailed
		if TEST:
			print "(@B@I%s@I@B)@B@r<<<@x __init__@B" % __name__

	def doCall(self, interfaceMethodName, *interfaceMethodArguments):
		if TEST:
			print "(@B@I%s@I@B)@B@g>>>@x doCall" % __name__
			print "(@B@I%s@I@B)\ttry calling %s.%s..." % (__name__, self.__toolModuleName, interfaceMethodName)
		if self.__toolModule != None and self.__toolModule.__dict__.has_key(interfaceMethodName):
			if TEST:
				print "(@B@I%s@I@B)\t\t@B@gfound@x@B" % __name__
			return self.__toolModule.__dict__[interfaceMethodName](interfaceMethodArguments)
		else:
			if TEST:
				print "(@B@I%s@I@B)\t\t@B@gfailed@x@B" % __name__
			raise CommonCallWrapperInterfaceNotAvailable

	def translatePathnames(self, stringToTranslate):
		if TEST:
			print "(@B@I%s@I@B)@B@g>>>@x translatePathnames" % __name__
			print "(@B@I%s@I@B)\ttry calling %s.%s..." % (__name__, self.__toolModuleName, "translatePathnames")
		if self.__toolModule != None and self.__toolModule.__dict__.has_key("translatePathnames"):
			if TEST:
				print "(@B@I%s@I@B)\t\t@B@gfound@x@B" % __name__
				print "(@B@I%s@I@B)@B@r<<<@x translatePathnames" % __name__
			return self.__toolModule.translatePathnames(stringToTranslate)
		else:
			if TEST:
				print "(@B@I%s@I@B)\t\t@B@gfailed@x@B" % __name__
				print "(@B@I%s@I@B)@B@r<<<@x translatePathnames" % __name__
			return stringToTranslate

# Exception classes definition
import exceptions
class CommonCallWrapperInitFailed(exceptions.StandardError):
	pass

class CommonCallWrapperInterfaceImportFailed(exceptions.StandardError):
	pass

class CommonCallWrapperInterfaceNotAvailable(exceptions.StandardError):
	pass

